package za.co.yellowfire.solarflare.web.ui;

import org.primefaces.component.datagrid.DataGrid;
import org.primefaces.event.SelectEvent;
import za.co.yellowfire.solarflare.*;
import za.co.yellowfire.solarflare.web.model.PersonalContact;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Mark P Ashworth
 * @version 0.0.1
 */
@Named
@SessionScoped
public class IndexController extends AbstractCommonUIController {
    private static final long serialVersionUID = 1L;

    private String search;
    private SearchResult searchResult;
    private Object selected;
    private DataGrid grid;

    @EJB(beanName = "SearchManager", beanInterface = SearchManager.class)
    private SearchManager searchManager;

    @PostConstruct
    public void init() {
        /*Create an empty contact so that Mojarra 2.1.3 on JBoss AS7.1.0 Beta does not fail*/
        selected = new PersonalContact();
    }

    public SearchResult getSearchResult() {
        return searchResult;
    }

    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }

    /**
     * The selected object for viewing
     * @return The selected object
     */
    public Object getSelected() {
        return selected;
    }

    /**
     * The selected object for viewing
     * @param selected The new selected object
     */
    public void setSelected(Object selected) {
        this.selected = selected;
    }

    /**
     * A binding to the grid on the view
     * @return The grid instance
     */
    public DataGrid getGrid() {
        return grid;
    }

    /**
     * A binding to the grid on the view
     * @param grid The grid instance
     */
    public void setGrid(DataGrid grid) {
        this.grid = grid;
    }

    /**
     * Searches
     * @param event The action event
     */
    public void onSearch(ActionEvent event) {
        SearchQuery query = new SearchQuery(PersonalContact.class, getSearch());
        query.setOperator(QueryOperator.AND);
        this.searchResult = searchManager.search(query);
    }

    /**
     * Suggests searches
     * @param text The search text to suggest alternatives
     * @return A list of suggestions
     */
    public List<String> onSuggest(String text) {
        List<String> results = new ArrayList<String>();

        if (text != null) {
            SearchQuery query = new SearchQuery(PersonalContact.class, text.toLowerCase());
            query.setOperator(QueryOperator.AND);

            SearchResult result = searchManager.search(query);
            if (result.size() == 0) {
                for(SearchSuggestion suggestion : result.getSuggestions()) {
                    for (String alternative : suggestion.getAlternatives()) {
                        result = searchManager.search(new SearchQuery(PersonalContact.class, alternative));
                        for (Object contact : result) {
                            results.add(((PersonalContact) contact).getName().getFullName());
                        }
                    }
                }
            } else {
                for (Object contact : result) {
                    results.add(((PersonalContact) contact).getName().getFullName());
                }
            }
        }
        return results;
    }

    public void onSuggestSelect(SelectEvent event) {
        SearchQuery query = new SearchQuery(PersonalContact.class, getSearch());
        query.setOperator(QueryOperator.AND);
        this.searchResult = searchManager.search(query);

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected:" + event.getObject().toString(), null);
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
}
